%unsort_deg(Xs,D).

unsort_deg([],0):-!.
unsort_deg(L,N):-
  unsort_deg(L,N,0).
  
unsort_deg([],N,N):-!.
unsort_deg([X|T],N,Acc):-
    check(X,T,SubAcc),
    Acc1 is SubAcc +Acc,
    unsort_deg(T,N,Acc1).
    
check(X,List,Acc):-
    check(X,List,Acc,0).
    
check(_,[],N,N):-!.
check(X,[H|T],N,Acc):-
    X > H,!,
    Acc1 is Acc + 1,
    check(X,T,N,Acc1).
check(X,[H|T],N,Acc):-
    X =< H,!,
    check(X,T,N,Acc).